local UIBase = class("UIBase")
local GameUtils = require("utils.GameUtils")
local Vector3 = require("utils.Vector3")

--[[
local resource={
    path= "xxx",
    name="xxx",
    canvas= xx --1- fixcanvas 2-uicanvas 3-popcanvas
}
]]
function UIBase:ctor(resource)
    local prefabPath = resource.path .. "/" .. resource.name
    local parentCanvas = nil
    if resource.parent then
        parentCanvas = resource.parent
    elseif resource.canvas == 1 then
        parentCanvas = G_UIManager:getFixCanvas()
    elseif resource.canvas == 2 then
        parentCanvas = G_UIManager:getUICanvas()
    elseif resource.canvas == 3 then
        parentCanvas = G_UIManager:getPopCanvas()
    end
    -- if resource.parent then
    local prefabs = G_ResourceManager:loadPrefabs(prefabPath)
    local gameObject = GameUtils.instantiate(prefabs, parentCanvas.transform)
    local luaBehaviour = gameObject:GetComponent(typeof(CS.LuaBehaviour))
    if luaBehaviour then
        local scriptEnv = luaBehaviour:GetScriptEnv()
        assert(scriptEnv == nil, "load gameobject by Uibase scriptEnv must nil")
        luaBehaviour:SetScriptEnv(self)
    else
        assert(false, "can not load gameobject by Uibase the LuaBehaviour not found")
    end
    -- else
    --     self._objectPool = G_ObjectPoolManager:addResObjectPool(prefabPath) -- GameUtils.instantiate(object, parentCanvas.transform)
    --     local gameObject, bStart = self._objectPool:getResObject(parentCanvas.transform)
    --     local luaBehaviour = gameObject:GetComponent(typeof(CS.LuaBehaviour))
    --     if luaBehaviour then
    --         local scriptEnv = luaBehaviour:GetScriptEnv()
    --         assert(scriptEnv == nil, "load gameobject by Uibase scriptEnv must nil")
    --         luaBehaviour:SetScriptEnv(self, bStart)
    --     else
    --         assert(false, "can not load gameobject by Uibase the LuaBehaviour not found")
    --     end
    -- end
end

function UIBase:close()
    self:onClose()
    if self.isDestroy then
        return
    end
    if self._objectPool and not self._objectPool.isDestroy then
        self._objectPool:recycleObject(self.gameObject)
    else
        self._objectPool = nil
        GameUtils.destroy(self.gameObject)
    end
end

function UIBase:onClose()
end

function UIBase:onDestroy()
end

function UIBase:SetActive(active)
    self.gameObject:SetActive(active)
end

function UIBase:isActive()
    return self.gameObject.active
end

return UIBase
